package org.hyh.core.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * @auther :hyh
 * @desc :
 * @date :2020/7/12
 */
@Service
@Lazy(false)
public class SpringUtils  implements ApplicationContextAware {

    private static   ApplicationContext  ac;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
       ac=applicationContext;
    }

    /**
     * 获取数据类型
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T>  T getBeanByClass(Class<T> tClass) {
       return   (T)ac.getBean(tClass);
    }

    /**
     * 发布事件
     * @param event
     */
    public  static   void   publishEvent(ApplicationEvent event){
        ac.publishEvent(event);
    }


}
